/**
 * 
 */
package com.android.calldelay.services;

import java.lang.reflect.Method;


import com.android.calldelay.AppInfo;
import com.android.internal.telephony.ITelephony;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

/**
 * @author INVEERA
 *
 */
public class CallDisconnectService extends Service {

	private static final String LOG_TAG = null;
	private ITelephony telephonyService;

	/* (non-Javadoc)
	 * @see android.app.Service#onBind(android.content.Intent)
	 */
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see android.app.Service#onCreate()
	 */
	@Override
	public void onCreate() {
		super.onCreate();
		
	}

	/* (non-Javadoc)
	 * @see android.app.Service#onDestroy()
	 */
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/* (non-Javadoc)
	 * @see android.app.Service#onStart(android.content.Intent, int)
	 */
	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		endCurrentCall();
		//showToast("Servece is started");
	}

	private void showToast(String msg) {
		Toast.makeText(CallDisconnectService.this,msg, Toast.LENGTH_SHORT).show();
		
	}

	private void endCurrentCall() {


		TelephonyManager tm = (TelephonyManager)
				CallDisconnectService.this.getSystemService(Context.TELEPHONY_SERVICE);
		try {
			// Java reflection to gain access to TelephonyManager's
			// ITelephony getter
			Log.v("Tag", "Get getTeleService...");
			Class c = Class.forName(tm.getClass().getName());
			Method m = c.getDeclaredMethod("getITelephony");
			m.setAccessible(true);
			telephonyService = (ITelephony)m.invoke(tm);
		} catch (Exception e) {
			e.printStackTrace();
			Log.e(LOG_TAG,
					"FATAL ERROR: could not connect to telephony subsystem");
			Log.e(LOG_TAG, "Exception object: " + e);
		}


		try {

			telephonyService.endCall();
			Log.d(LOG_TAG, "call is disconnected......."+AppInfo.FLAG_CALL_IS_DISCONNECTED);




		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	 
		
	}

}
